//
//  LookBooks.m
//  dogBook
//
//  Created by 黄辉 on 16/12/12.
//  Copyright © 2016年 黄辉. All rights reserved.
//

#import "LookBooks.h"

@interface LookBooks ()

@end

@implementation LookBooks

@synthesize str;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect re1 = [UIScreen mainScreen].bounds;
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, re1.size.height - 44, re1.size.width, 44)];
    toolBar.tintColor = [UIColor grayColor];
    [self.view addSubview:toolBar];
    
    textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, re1.size.width, re1.size.height - 44)];
    [self.view addSubview:textView];
    textView.text = self.str;
    
    textView.font = [UIFont italicSystemFontOfSize:18];
    textView.textColor = [UIColor blackColor];
    textView.backgroundColor = [UIColor grayColor];
    textView.editable = NO;
    
    allPage = textView.contentSize.height;
    
    currentPage = 1;
    
    UIBarButtonItem *button01 = [[UIBarButtonItem alloc]initWithTitle:@"狗书书架" style:UIBarButtonItemStylePlain target:self action:@selector(onClick)];
    UIBarButtonItem *button02 = [[UIBarButtonItem alloc]initWithTitle:@"上一页" style:UIBarButtonItemStylePlain target:self action:@selector(upPage)];
    UIBarButtonItem *button03 = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(downPage)];
    button04 = [[UIBarButtonItem alloc]initWithTitle:[NSString stringWithFormat:@"%d/%d",currentPage,allPage] style:UIBarButtonItemStylePlain target:self action:@selector(hello)];
    NSArray *arrry = [[NSArray alloc]initWithObjects:button01,button02,button03,button04, nil];
    [toolBar setItems:arrry animated:YES];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
-(void)upPage{
    
    
    if (currentPage == 1) {
        UIAlertController *alertcontroller = [UIAlertController alertControllerWithTitle:@"狗书提示" message:@"这已是第一页" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cencel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
        [alertcontroller addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"登录";
        }];
        [alertcontroller addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"密码";
            textField.secureTextEntry = YES;
        }];
        
        [alertcontroller addAction:cencel];
        [alertcontroller addAction:confirm];
        [self presentViewController:alertcontroller animated:YES completion:nil];
        return;
    }
    currentPage = currentPage - 1;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.1];
    [textView setContentOffset:CGPointMake(0, (currentPage - 1)) animated:YES];
    [UIView commitAnimations];
    //
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
    
    
    [UIView commitAnimations];
    
    [button04 setTitle:[NSString stringWithFormat:@"%d/%d",currentPage,allPage]];
    return;
}
-(void)downPage{
    if (currentPage == allPage) {
        UIAlertController *alertcontroller = [UIAlertController alertControllerWithTitle:@"狗书提示" message:@"这已是最后一页" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cencel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:nil];
        [alertcontroller addAction:cencel];
        [alertcontroller addAction:confirm];
        [alertcontroller presentedViewController];
        
        return;
    }
    currentPage = currentPage + 1;
    
    [button04 setTitle:[NSString stringWithFormat:@"%d/%d",currentPage ,allPage]];
    
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.1];
    [textView setContentOffset:CGPointMake(0, (currentPage - 1) *430) animated:YES];
    [UIView commitAnimations];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    [UIView commitAnimations];
    
    return;
}

-(void)hello{
    return;
}
- (void)onClick{
    [self dismissViewControllerAnimated:YES completion:
     ^{
         
     }];
    return;
}


@end
